home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / SETTIME.ZIP / CWINSOCK.H < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  4.9 KB  |  153 lines

  1. // cwinsock.h : header file
  2. //
  3.  
  4. #ifndef __CWINSOCK_H__
  5. #define __CWINSOCK_H__
  6.  
  7. #include <winsock.h>
  8.  
  9. // return values for most member functions
  10. #define CWINSOCK_NOERROR            (0)
  11. #define CWINSOCK_WINDOWS_ERROR      (1)
  12. #define CWINSOCK_WINSOCK_ERROR      (2)
  13. #define CWINSOCK_PROGRAMMING_ERROR  (3)
  14.  
  15. // message to be posted to the socket window for async events
  16. #define CWINSOCK_EVENT_NOTIFICATION (WM_USER + 1)
  17.  
  18. // wParam of messages sent to application windows
  19. #define CWINSOCK_DONE_WRITING               (1) // lParam = pointer to data
  20. #define CWINSOCK_ERROR_WRITING              (2) // lParam = pointer to data
  21. #define CWINSOCK_DONE_READING               (3) // lParam = # data chunks in queue
  22. #define CWINSOCK_ERROR_READING              (4)
  23. #define CWINSOCK_READY_TO_ACCEPT_CONNECTION (5)
  24. #define CWINSOCK_YOU_ARE_CONNECTED          (6)
  25. #define CWINSOCK_LOST_CONNECTION            (7)
  26.  
  27. // buffer sizes
  28. #define READ_BUF_LEN (1000)
  29.  
  30. // structure used for datagram socket read/write queue
  31. typedef struct tagDATAGRAMDATA
  32. {
  33.   LPVOID      pData;
  34.   int         nLen;
  35.   SOCKADDR_IN sin;
  36. } DATAGRAMDATA, FAR * LPDATAGRAMDATA;
  37.  
  38. // structure used for stream socket read/write queue
  39. typedef struct tagSTREAMDATA
  40. {
  41.   LPVOID pData;
  42.   int    nLen;
  43. } STREAMDATA, FAR * LPSTREAMDATA;
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CWinSock
  47. //
  48. class CWinSock
  49. {
  50. private:
  51.   WORD m_wVersionRequired;   // WinSock version required by application
  52.   int m_nLastError;          // last WinSock error
  53.   WSADATA m_wsaData;         // WinSock information
  54.  
  55. public:
  56.   CWinSock(WORD wVersionRequired = MAKEWORD(1, 1));
  57.   int Startup();
  58.   int Shutdown();
  59.   void Information(LPWSADATA pwsaData);
  60.   int LastError() { return m_nLastError; }
  61. };
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CDatagramSocket
  65. //
  66. class CDatagramSocket : public CWnd
  67. {
  68. private:
  69.   CWnd *m_pParentWnd;      // window to receive event notification
  70.   UINT m_uMsg;             // message to send to m_pParentWnd on event
  71.   SOCKET m_s;              // socket handle
  72.   SOCKADDR_IN m_sinLocal;  // name bound to socket m_s
  73.   int m_nLastError;        // last WinSock error
  74.   BOOL m_bServer;          // TRUE if socket m_s is bound to a name
  75.   CPtrList m_listWrite;    // data waiting to be sent
  76.   CPtrList m_listRead;     // data read
  77.  
  78. public:
  79.   CDatagramSocket(CWnd *pParentWnd, UINT uMsg);
  80.   virtual ~CDatagramSocket();
  81.   int CreateSocket(int nLocalPort);
  82.   int CreateSocket(LPSTR pszLocalService = NULL);
  83.   int DestroySocket();
  84.   int Write(int nLen, LPVOID pData, LPSTR pszRemoteName, int nRemotePort);
  85.   int Write(int nLen, LPVOID pData, LPSTR pszRemoteName, LPSTR pszRemoteService);
  86.   int Write(int nLen, LPVOID pData, LPSOCKADDR_IN psinRemote);
  87.   LPVOID Read(LPINT pnLen, LPSOCKADDR_IN psinRemote = NULL);
  88.   int LastError() { return m_nLastError; }
  89.  
  90. private:
  91.   void InitVars(BOOL bInitLastError = TRUE);
  92.   LONG HandleRead(WPARAM wParam, LPARAM lParam);
  93.   LONG HandleWrite(WPARAM wParam, LPARAM lParam);
  94.  
  95.   // message map functions
  96. protected:
  97.   //{{AFX_MSG(CStreamSocket)
  98.   //}}AFX_MSG
  99.   LONG OnWinSockEvent(WPARAM wParam, LPARAM lParam);
  100.   DECLARE_MESSAGE_MAP()
  101. };
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CStreamSocket
  105. //
  106. class CStreamSocket : public CWnd
  107. {
  108. private:
  109.   CWnd *m_pParentWnd;      // window to receive event notification
  110.   UINT m_uMsg;             // message to send to m_pParentWnd on event
  111.   SOCKET m_s;              // socket handle
  112.   SOCKADDR_IN m_sinLocal;  // name bound to socket m_s
  113.   SOCKADDR_IN m_sinRemote; // name on other side of m_s
  114.   int m_nLastError;        // last WinSock error
  115.   BOOL m_bServer;          // TRUE if socket m_s is bound to a name
  116.   CPtrList m_listWrite;    // data waiting to be sent
  117.   CPtrList m_listRead;     // data read
  118.  
  119. public:
  120.   CStreamSocket(CWnd *pParentWnd, UINT uMsg);
  121.   virtual ~CStreamSocket();
  122.   int CreateSocket(int nLocalPort);
  123.   int CreateSocket(LPSTR pszLocalService = NULL);
  124.   int DestroySocket();
  125.   int Connect(LPSTR pszRemoteName, int nRemotePort);
  126.   int Connect(LPSTR pszRemoteName, LPSTR pszRemoteService);
  127.   int Connect(LPSOCKADDR_IN psinRemote);
  128.   int Accept(CStreamSocket *pStreamSocket);
  129.   int Write(int nLen, LPVOID pData);
  130.   LPVOID Read(LPINT pnLen);
  131.   int GetPeerName(LPSOCKADDR_IN psinRemote);
  132.   int LastError() { return m_nLastError; }
  133.  
  134. private:
  135.   void InitVars(BOOL bInitLastError = TRUE);
  136.   LONG HandleRead(WPARAM wParam, LPARAM lParam);
  137.   LONG HandleWrite(WPARAM wParam, LPARAM lParam);
  138.  
  139.   // message map functions
  140. protected:
  141.   //{{AFX_MSG(CStreamSocket)
  142.   //}}AFX_MSG
  143.   LONG OnWinSockEvent(WPARAM wParam, LPARAM lParam);
  144.   DECLARE_MESSAGE_MAP()
  145. };
  146.  
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CWinSockErrorBox
  149. //
  150. void CWinSockErrorBox(int nError, LPSTR pszMessage = NULL);
  151.  
  152. #endif // __CWINSOCK_H__
  153.